Function.prototype.mybind = function (thisArg, ...args) {
  var fn = this;

  thisArg = thisArg !== null && this !== undefined ? Object(thisArg) : window;

  var proxyFn = function (...args2) {
    thisArg.fn = fn;
    // const argArray = [...args, ...args2];
    var result = thisArg.fn(...args, ...args2);
    delete thisArg.fn;
    return result;
  };

  return proxyFn;
};

function foo() {
  console.log("foo被执行", this);
  return 20;
}

function sum(num1, num2, num3, num4) {
  console.log(num1, num2, num3, num4);
}

// 系统的bind使用
var bar = foo.bind("abc");
bar();

var newSum = sum.bind("aaa", 10, 20, 30, 40);
newSum();

var newSum = sum.bind("aaa");
newSum(10, 20, 30, 40);

var newSum = sum.bind("aaa", 10);
newSum(20, 30, 40);

// 使用自己定义的bind
var bar = foo.mybind("abc");
var result = bar();
console.log(result);

var newSum = sum.mybind("abc", 10, 20);
var result = newSum(30, 40);
